﻿function PostIt(form, success) {
    $.ajax({
        cache: false,
        async: true,
        type: "POST",
        url: $(form).attr('action'),
        data: $(form).serialize(),
        success: success
    });
}

function PostItWithContext(form, success, ctx) {
    $.ajax({
        cache: false,
        async: true,
        type: "POST",
        context: ctx,
        url: $(form).attr('action'),
        data: $(form).serialize(),
        success: success
    });
}

function PostItwithParams(form, success, params) {
    var url = $(form).attr('action');
    $.ajax({
        cache: false,
        async: true,
        type: "POST",
        url: url,
        data: params,
        success: success
    });
}

function RedirectToLogin() {
    var returnURL = encodeURIComponent(window.location.pathname);
    // ToDo: Get the login url dynamically.
    window.location.assign("/rated/Account/Login?ReturnUrl=" + returnURL);
}

function UserIsLoggedIn() {
    return $("#loginLink").length == 0; // logged in if we don't have a Login link (Obv.!)
}

$(function () {
    $(".itemRating").css("display", "block"); // show all the reviews!
    $(".ratingsList").css("display", "block"); // show all the reviews!
    $("#btnAddReview").on("click", function () {
        if (UserIsLoggedIn()) {
            if ($("#AddReviewPanel").css("display") != "block")
                $("#AddReviewPanel").slideDown("fast");
        }
        else {
            // redirect to the login page. The return URL will be set automatically.
            RedirectToLogin();
        }
    });

    $("#btnCancelReview").on("click", function () {
        $("#AddReviewPanel").slideUp("fast");
    });

    $("#btnPostNewReview ").on("click", function () {
        PostIt($('#addReview'), function (data) {
            $("#itemRatingsListContainer").prepend($(data.view).fadeIn('slow'));
            $("#AddReviewPanel").fadeOut("slow");
            $(".reviewCount").html(data.reviewCount);
            // ToDo: now clear the form of the existing values
        });
        return false;
    });

    $("#item").on("click", '.btnEditReview', function () {
        var reviewEditorform = $(this).siblings(".reviewEditorform");
        $(reviewEditorform).slideDown("fast");
        $(this).hide();
    });

    $("#item").on("click", '.btnCancelUpdateReview', function () {
        var reviewEditorform = $(this).parents(".reviewEditorform");
        var btnEditReview = $(reviewEditorform).siblings(".btnEditReview");
        $(reviewEditorform).slideUp("fast");
        $(".btnEditReview").show();
    });


    $("#item").on("click", '.btnUpdateReview', function () {
        PostIt($('.editReview', this), function (data) {
            alert("sdsdsd");
        });
        return false;
    });





    $("#ratedItemListContainer").on("click", "#btnUpdateUserProfile", function () {
        PostIt($('#updateUserProfile'), function (data) {
        });
        return false;
    });



    $("#ratedItemListContainer").on("change", ".ratedItemAttribute", function () {
        var attId = $(this).val();
        var checked = this.checked;
        var params = "ID=" + attId + "&selected=" + checked;
        var form = $(this).parents("form");
        var itemID = $("#Item_ID", form).val();
        params = params + "&Item.ID=" + itemID;
        PostItwithParams($(form), function (data) {
        }, params);
    });


    // wire up the ajax calls when a user changes the value of one of the drop down lists
    $("#itemLocation").on("change", ".ContinentID, .CountryID, .LocalityID, .StateID, .CityID", function () {
        var success = function (data) {
            $("#LocationFormContainer").replaceWith(data);
        };
        PostItWithContext($(this).parents("#updateLocation"), success, $(this));
        return false;
    });

    $(".btnDeleteReview ").on("click", function () {
        if (confirm("Are you sure you want to delete this review?")) {
            var success = function (data) {
                $(this).fadeOut("slow", function () {
                    $(this).remove();
                })
            };
            PostItWithContext($(this).parents(".deleteReview"), success, $(this).parents(".itemRating"));
        }
        return false;

    });

    $(".btnUpdateItemDescription ").on("click", function () {
        var success = function (data) {
            alert("done");
        }
        PostIt($(this).parents("form"), success);
        return false;
    });



    $(".btnRespond").on("click", function () {
        var success = function (data) {
            $("#itemRatingsListContainer").prepend($(data).fadeIn('slow'));
            $("#AddReviewPanel").fadeOut("slow");
        };
        PostIt($(this).parents(".respondToReview"), success);
        return false;
    });


    $(".gotoReviews").on("click", function () {
        $('html, body').animate({
            scrollTop: $("#Ratings").offset().top
        }, 500);
    });


    $(".likeMe").on("click", function () {
        alert("ToDo: implement LikeMe functionality");
    });


    $(".tabs").tabs();

    $("li.ratedItemTitle .expander").on("click", function () {
        var li = $(this).parents("li");
        var itemToSlide = $("ul", $(li));
        if ($(itemToSlide).css("display") == "block") {
            // if we are visible, then hide
            $(itemToSlide).slideUp("fast");
            $(this).html("+");
        }
        else {
            // else show
            $(itemToSlide).slideDown("fast");
            $(this).html("-");
        }
    })
});